PHP - Sessions

    PHP sessions allow you to access data among different pages of an entire website. A temporary file has been created by the session that is stored at the server along with its data and the session variables and the information will be then available to all pages. You can decide where to store this session file by setting the value of session.save_path within the php.ini file. Once you staart a session below are the process that starts:

    • A random string of 32 hexadecimal number unique identifier is created for that session.
    • This unique string is stored in an automatically sent cookie called PHPSESSID.
    • It will create a file that will be stored on the designated location set in the .ini file and the name is assigned as the unique identifier prefixed with sess_.

    How to start a PHP session

    PHP supports a session_start() function that will start the session. If there is no running session then this function will create one but it is necessary to call the function at the beginning for creating the session. All the information about session is stored within $_SESSION[] associative array which can be accessed during a lifetime. isset() function can be used to check if the session variable is set or not.

    Example:

    <?php
    session_start();
    if( isset( $_SESSION['counter'] ) ) {
    $_SESSION['counter'] += 1;
    }else {
    $_SESSION['counter'] = 1;
    }
    $msg = "already visited". $_SESSION['counter'];
    $msg .= "in this session.";
    ?>
    <html>
    <head>
    <title>PHP session</title>
    </head>
    <body>
    <?php echo ( $msg ); ?>
    </body>
    </html>

    Output: already visited in this session

    How to destroy a PHP session

    PHP supports the use of destroy_session() function that is used to destroy the session. There is no need to pass any argument if you call this function all the sessions running will get destroyed. In case if you are looking to destroy a particular session then you can use unset() function for a particular session variable. Example: Unsetting a session variable:

    <?php
    unset($_SESSION['counter']);
    ?>

    Destroying all session

    <?php
    session_destroy();
    ?>

    If you want that a session will create automatically without using the start_session() function then you can set session.auto_start variable to 1 within the php.ini file.

    Creating session without cookies

    If the user is not allowing cookies to set on its system then there is another way to send the session ID to the browser. In that case, you can use the constant SID which is defined if you start a session. If the client does not send session cookie then it will use an empty string.

    Example:

    <?php
    session_start();
    if (isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 1;
    }else {
    $_SESSION['counter']++;
    }
    $msg = "You have visited this page ". $_SESSION['counter'];
    $msg .= "in this session.";
    echo ( $msg );
    ?>

    Output:

    You have visited this page 1in this session.

    People are also reading: